﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.ServiceModel;

namespace Client
{
    public partial class FormLogOn : BaseForm
    {
        public FormLogOn()
        {
            InitializeComponent();
            this.NecessaryRemark(this.txt_pwd, this.txt_UserName);
        }

        private void btn_Logon_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txt_UserName.Text.Trim()))
            {
                GlobalInfo.MsgBoxShowError("请输入用户名");
                return;
            }
            if (string.IsNullOrEmpty(this.txt_pwd.Text.Trim()))
            {
                GlobalInfo.MsgBoxShowError("请输入密码");
                return;
            }
            try
            {
                QunYing.Common.IOnline opera = QunYing.Common.ClientProxyFactory.GetProxy<QunYing.Common.IOnline>(QunYing.Common.GlobalInfo.ServiceIsOnline);
                opera.IsOnline();
                (opera as ICommunicationObject).Close();
            }
            catch (Exception)
            {

                GlobalInfo.MsgBoxShowError("网络访问失败,如果你的电脑能访问网页,则请联系系统管理员!");
                return;
            }
            QunYing.Common.Computer cm = QunYing.Common.Computer.Instance();
            string validInfo = cm.CpuID + "--" + cm.DiskID + "--" + cm.MacAddress;
            QunYing.BLL.UserLoginInfo userLoginInfo = new QunYing.BLL.UserLoginInfo();
            QunYing.BLL.Employee bll_employee = new QunYing.BLL.Employee();
            var employee = bll_employee.GetModel(this.txt_UserName.Text.Trim());
            if (employee != null)
            {
                if (employee.密码 != this.txt_pwd.Text.Trim())
                {
                    GlobalInfo.MsgBoxShowError("密码错误！");
                    return;
                }
                else if(!employee.是否启用)
                {
                    GlobalInfo.MsgBoxShowError("帐号已经被禁用");
                }
                else if(employee.验证信息==null)
                {
                    employee.验证信息 = validInfo;
                    bll_employee.Update(employee);
                }
                else if (employee.验证信息 != validInfo)
                {
                    GlobalInfo.MsgBoxShowError("验证信息错误，请和管理员联系，申请解除绑定！");
                    return;
                }
                QunYing.Model.UserLoginInfo loginInfpo = userLoginInfo.GetModel(employee.所属经销商);
                if (loginInfpo == null)
                {
                    GlobalInfo.MsgBoxShowError("您没有权限登录该系统");
                    return;
                }
                else
                {
                    GlobalInfo.UserLoginInfo = loginInfpo;
                    GlobalInfo.IsEmployee = true;
                    this.DialogResult = DialogResult.Yes;
                    return;
                }
            }

            QunYing.Model.UserLoginInfo uli = userLoginInfo.GetModel(this.txt_UserName.Text.Trim());
            if (uli == null)
            {
                GlobalInfo.MsgBoxShowError("用户名或密码错误！");
                return;
            }
            if (uli.密码 != this.txt_pwd.Text.Trim())
            {
                GlobalInfo.MsgBoxShowError("密码错误！");
                return;
            }
            if (uli.角色 == "会员")
            {
                GlobalInfo.MsgBoxShowError("您没有权限登入该系统！");
                return;
            }
            if (uli.验证信息 == null)
            {
                uli.验证信息 = validInfo;
                userLoginInfo.Update(uli);
            }
            else
            {

                  string str = validInfo;
                if (str != uli.验证信息)
                {
                    GlobalInfo.MsgBoxShowError("验证信息不正确，请与管理员联系，申请解除软件验证！");
                    return;
                }
                 

            }
            GlobalInfo.UserLoginInfo = uli;
            this.DialogResult = DialogResult.Yes;
        }



        private void btn_FindPwd_Click(object sender, EventArgs e)
        {

            GlobalInfo.MsgBoxShowInfo("请和管理员联系！");
        }
    }
}